package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.ChemicalCompositionsModel;
import application.model.modify.ChemicalCompositionModel;
import application.translators.Translator;
import application.translators.impl.ChemicalCompositionsTranslator;
import dao.ChemicalCompositionDao;
import mappings.ChemicalComposition;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("chemicalCompositions")
public class ChemicalCompositionsService
{
    private ChemicalCompositionDao chemicalCompositionDao = new ChemicalCompositionDao();
    private Translator<List<ChemicalComposition>, ChemicalCompositionsModel> translator = new ChemicalCompositionsTranslator();

    /*
     * Zwraca wszystkie chemical_compositions w postaci XML dla zadanego material_id
     */
    @Path("getForMaterial/{materialId}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public ChemicalCompositionsModel getChemicalCompositionsForMaterial(@PathParam("materialId") int materialId)
    {
        return translator.translate(chemicalCompositionDao.getChemicalCompositionsForMaterial(materialId));
    }

    @Path("addChemicalComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ChemicalCompositionModel addChemicalComposition(@FormParam("materialId") int materialId,
                                                             @FormParam("chemicalElementId") int chemicalElementId,
                                                            @FormParam("min") double min,
                                                            @FormParam("max") double max)
    {
        ChemicalCompositionModel chemicalCompositionModel = new ChemicalCompositionModel();

        try
        {
            ChemicalComposition chemicalComposition = chemicalCompositionDao.addChemicalComposition(materialId, chemicalElementId, min, max);
            chemicalCompositionModel.setShortcut(chemicalComposition.getChemicalElements().getShortcut());
            chemicalCompositionModel.setMin(chemicalComposition.getMin());
            chemicalCompositionModel.setMax(chemicalComposition.getMax());
            chemicalCompositionModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            chemicalCompositionModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            chemicalCompositionModel.setStatus(2);
        }

        return chemicalCompositionModel;
    }

    @Path("editChemicalComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ChemicalCompositionModel editProducent(@FormParam("id") Integer id,
                                                 @FormParam("materialId") int materialId,
                                                 @FormParam("chemicalElementId") int chemicalElementId,
                                                 @FormParam("min") double min,
                                                 @FormParam("max") double max)
    {
        ChemicalCompositionModel chemicalCompositionModel = new ChemicalCompositionModel();

        try
        {
            ChemicalComposition chemicalComposition = chemicalCompositionDao.editChemicalComposition(id, materialId, chemicalElementId, min, max);
            chemicalCompositionModel.setShortcut(chemicalComposition.getChemicalElements().getShortcut());
            chemicalCompositionModel.setMin(chemicalComposition.getMin());
            chemicalCompositionModel.setMax(chemicalComposition.getMax());
            chemicalCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            chemicalCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            chemicalCompositionModel.setStatus(2);
        }

        return chemicalCompositionModel;
    }

    @Path("deleteChemicalComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ChemicalCompositionModel deleteChemicalComposition(@FormParam("id") Integer id)
    {
        ChemicalCompositionModel chemicalCompositionModel = new ChemicalCompositionModel();

        try
        {
            chemicalCompositionDao.deleteChemicalComposition(id);
            chemicalCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            chemicalCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            chemicalCompositionModel.setStatus(2);
        }

        return chemicalCompositionModel;
    }
}
